package com.da.javatest.leetCode2;

import java.util.ArrayList;
import java.util.List;

public class Code6 {
    public static void main(String[] args) {
        new Code6().convert("AB", 1);
    }

    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        List<List<Character>> lists = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            lists.add(new ArrayList<>());
        }
        boolean flag = true;
        int index = 1;
        for (char c : s.toCharArray()) {
            if (flag) {
                lists.get(index - 1).add(c);
                if (index == numRows) {
                    flag = false;
                    index--;
                } else {
                    index++;
                }
            } else {
                lists.get(index - 1).add(c);
                if (index == 1) {
                    flag = true;
                    index++;
                } else {
                    index--;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (List<Character> list : lists) {
            for (Character c : list) {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
